package vn.com.servicedirectory.controller;

import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import vn.com.servicedirectory.common.CommonVariable;
import vn.com.servicedirectory.forms.ServiceEditForm;
import vn.com.servicedirectory.forms.ServiceIndexFrom;
import vn.com.servicedirectory.service.ProgrammeService;

@Controller
public class ServiceController {
	@Inject
	protected ProgrammeService programmeService;
	
	@RequestMapping(value = CommonVariable.PAGE_SERVICE_INDEX_HTML, method = RequestMethod.GET)
	public String showIndex(Model model, HttpServletRequest request) {
		ServiceIndexFrom serviceIndexForm =new ServiceIndexFrom();
		serviceIndexForm.setFormTitle(CommonVariable.FORM_DEMO_TITLE);
		model.addAttribute("serviceIndex", programmeService.findByName("all",0, 5, true));
		return CommonVariable.PAGE_SERVICE_INDEX;
	}
	@RequestMapping(value = "serviceEdit")
	public ModelAndView serviceEdit() {
		return new ModelAndView(
				"redirect:serviceEditAndCreate.html?command=edit");
		// return new ModelAndView(CommonVariable.PAGE_SERVICEINDEX,
		// "welcomeMessage",CommonVariable.EDIT);
	}

	@RequestMapping(value = "serviceInActive")
	public ModelAndView serviceInActive() {
		return new ModelAndView(CommonVariable.PAGE_SERVICE_INDEX,
				"welcomeMessage", CommonVariable.IN_ACTIVE);
	}

	@RequestMapping(value = "serviceCreate")
	public ModelAndView serviceCreate() {
		return new ModelAndView(
				"redirect:serviceEditAndCreate.html?command=create");
		// return new ModelAndView(CommonVariable.PAGE_SERVICEINDEX,
		// "welcomeMessage",CommonVariable.CREATE);
	}
	@RequestMapping(value = CommonVariable.PAGE_SERVICE_EDIT_UPDATE_HTML, method = RequestMethod.GET)
	public String showFormEditUpdate(Map<String, ServiceEditForm> model,
			HttpServletRequest httpRequest) {
	/*	if(httpRequest.getParameter("command").equals("edit")){
			//To do edit
		}else{
			if(httpRequest.getParameter("command").equals("create")){
				//To do create
		}*/
		ServiceEditForm serviceEditForm = new ServiceEditForm();
		model.put("serviceEditForm", serviceEditForm);
		return CommonVariable.PAGE_SERVICE_EDIT_UPDATE;
	}

	@RequestMapping(value = "serviceEditBack")
	public String back() {
		return "redirect:" +CommonVariable.PAGE_SERVICE_INDEX_HTML;
	}

	@RequestMapping(value = CommonVariable.PAGE_SERVICE_EDIT_UPDATE_HTML, method = RequestMethod.POST)
	public ModelAndView serviceDeactive(@Valid ServiceEditForm serviceEditForm,
			Map<String, ServiceEditForm> model, HttpServletRequest httpRequest) {
		model.put("serviceEditForm", serviceEditForm);
		return new ModelAndView(CommonVariable.PAGE_SERVICE_EDIT_UPDATE,
				"welcomeMessage", serviceEditForm.getProgrammeName()
						+ httpRequest.getParameter("command"));

	}
}
